<?php
namespace july\julyadmin\common\model;

use think\Model;

/*
文件分类模型
 */
class FilesCategory extends Model
{
    //主键
    protected $pk = 'id';
    //设置自动时间写入类型
    protected $autoWriteTimestamp = 'datetime';
    //设置创建时间字段
    protected $createTime = 'create_time';
    //设置更新时间字段
    protected $updateTime = 'update_time';
    //权限菜单tree数据
    private $permissionsTree = [];

    /**
     * 上级分类
     * @Author   July
     * @DateTime 2024-01-13T20:15:46+0800
     */
    public function parentcategory()
    {
        return $this->hasOne(FilesCategory::class, 'id', 'parent_id');
    }


    /**
     * 获取指定菜单所有上级id
     * @Author   July
     * @DateTime 2024-01-13T20:15:59+0800
     * @param  string $menu_id 菜单id
     * @return array  所有上级id一维数组
     */
    public static function getParentIds($menu_id = '', &$ids = [])
    {
        $parent_id = self::where(['id' => $menu_id])->value('parent_id');
        if ($parent_id) {
            $ids[] = $parent_id;
            self::getParentIds($parent_id, $ids);
        }
        return $ids;
    }

    /**
     * 获取所有下级分类名称
     * @Author   july
     * @DateTime 2023-09-25
     * @param    string     $id 分类id
     * @return   array      所有分类名称一维数组
     */
    public static function getChildrenNames($id = '', &$names = [])
    {
        $list = self::field('id,name')->where(['parent_id' => $id])->select()->toArray();
        foreach ($list as $k => $v) {
            $names[] = $v['name'];
            self::getChildrenNames($v['id'], $names);
        }
        return $names;
    }
}
